package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.JobCategory;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.JobCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class JobCategoryController {

    @Resource
    JobCategoryService jobCategoryService;

    // 删除职位类别



    // 添加职位类别
    @PostMapping("insertJobCategory")
    public ResponseText insertJobCategory(@RequestBody JobCategory jobCategory) {
        ResponseText responseText = null;
        try {
            jobCategory.setJobcatestatus(0);
            jobCategoryService.insertJobCategory(jobCategory);
            responseText = new ResponseText(null, 200, "添加职位类别成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 修改职位类别
    @PutMapping("updateJobCategory/{position}")
    public ResponseText updateJobCategory(@PathVariable("position") String position,
                                          @RequestBody JobCategory jobCategory) {
        ResponseText responseText = null;
        try {
            jobCategoryService.updateJobCategory(jobCategory, position);
            responseText = new ResponseText<>(null,200,"职位类别修改成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 查询所有职位类别
    @GetMapping("getAllJobCategory/{pageIndex}/{pageSize}")
    public ResponseText selectAllJobCategory(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize
                                       ){
        ResponseText<PageBean<JobCategory>> responseText = null;
        try {
            PageBean<JobCategory> allJobCategorypageBean = jobCategoryService.getAllJobCategory(pageIndex,pageSize);
            responseText = new ResponseText<>(allJobCategorypageBean,200,"查询到所有职位类别！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //提交删除申请
    @DeleteMapping("deleteJobcateApply/{position}")
    public ResponseText deleteJobcateApplyByPosition(@PathVariable("position") String position) {
        ResponseText responseText = null;
        try {
            jobCategoryService.deleteJobcateApplyByPosition(position);
            responseText = new ResponseText(null, 200, "提交删除职位类别申请成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //条件查询
    @PostMapping("getJobCategory/{pageIndex}/{pageSize}")
    public ResponseText getJobCategory(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize,
                                       @RequestBody JobCategory jobCategory) {
        ResponseText responseText = null;
        try {
            PageBean<JobCategory> jobCategorypageBean =  jobCategoryService.getJobCategory(jobCategory, pageIndex, pageSize);
            responseText = new ResponseText<>(jobCategorypageBean,200,"条件查询职位类别！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
